package com.project.nimsen.util;

import java.io.File;
import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Repository;

import com.project.nimsen.entity.Site;
import com.project.nimsen.entity.Syspro;
import com.project.nimsen.entity.User;
import com.project.nimsen.service.ISiteService;
import com.project.nimsen.service.ISysproService;
import com.project.jdbc.utils.DateUtils;
/**
 * web里的基础信息
 * */
@Repository("webUtil")
public class WebUtil {
	@Resource
	ISysproService sysproser;
	@Resource
	ISiteService siteService;
	/**
	 * 根据不同的系统目录得到相对应所在服务器里的路径
	 * */
	public String getSysPath(String sysname,HttpServletRequest request)throws Exception{
		ServletContext sevcon = request.getSession().getServletContext();
		//判断系统目录是否存在
		if(sevcon.getAttribute(sysname)==null){
			Syspro sys = sysproser.findSysByName(sysname);
			//生成pdf所在的全目录
			String allPath = sevcon.getRealPath("/"+sys.getSysprovalue());
			File dir = new File(allPath);
			//生成目录并设置到application里
			if (!dir.exists())
				dir.mkdirs();
			sevcon.setAttribute(sysname, allPath);
			return allPath;
		}
		return (String)sevcon.getAttribute(sysname);
	}
	/**
	 * 把相应的短路径存到application里
	 * */
	public void getWebPath(String sysname,HttpServletRequest request)throws Exception{
		ServletContext sevcon = request.getSession().getServletContext();
		//判断系统目录是否存在
		if(sevcon.getAttribute(sysname+"web")==null){
			Syspro sys = sysproser.findSysByName(sysname);
			sevcon.setAttribute(sysname+"web", sys.getSysprovalue());
		}
	}
	/**
	 * 生成随机字符串
	 * @author dubing
	 * */
	public String autoString(){
		StringBuffer str = new StringBuffer();
		//当前日期
		Date now = new Date();
		str.append(DateUtils.convertDate2String("yyyyMMddHHmmssSSS", now));
		//四位随机数
		String random = Math.random()+"";
		random = StringUtils.replace(random, ".", "");
		str.append(random);
		return str.toString();
	}
	/**
	 * 得到当前登录人信息
	 * */
	public User getCurrUser(){
		User use = (User)SecurityUtils.getSubject().getPrincipal();
		return use;
	}
	/**
	 * 把当前用户管理的站点放入session中
	 * 得到站点信息的key为mansite
	 * */
	public void addSessionSiteAttr(HttpServletRequest request)throws Exception{
		User use = getCurrUser();
		Site site = siteService.findEntityByUserid(use.getId());
		//把用户所管理的站点信息存入session中
		if(site!=null){
			HttpSession session = request.getSession();
			session.setAttribute("mansite", site);
		}
	}
	/**
	 * 得到微信的url
	 * @throws Exception 
	 * */
	public String wxurl(HttpServletRequest request) throws Exception{
		Site site = currSite(request);
		String domainname = site.getDomainname();
		StringBuffer url = new StringBuffer();
		if(domainname.contains("http://")||domainname.contains("https://")){
		}else{
			url.append("http://");
		}
		url.append(domainname).append("/pub/wx/pt/").append(site.getId());
		return url.toString();
	}
	/**
	 * 得到token信息
	 * @throws Exception 
	 * */
	public String wx_token(HttpServletRequest request) throws Exception{
		Site site = currSite(request);
		if(site==null)
			return null;
		String manid = site.getId();
		String token = manid.replaceAll("-", "");
		int length = token.length();
		return token.substring(length-32, 32);
	}
	public Site currSite(HttpServletRequest request)throws Exception{
		//以下是属性设置
		HttpSession session = request.getSession();
		Object obj = session.getAttribute("mansite");
		if(obj!=null)
			return (Site)obj;
		String domainname = request.getServerName();
		Site site = siteService.findEntityByDomainame(domainname);
		return site;
	}
	/**
	 * 得到网站的根目录
	 * */
	public static String webUrl(HttpServletRequest request){
		String domainname = request.getServerName();
		HttpSession session = request.getSession();
		if(session.getAttribute("ctx")==null){
			//得到配置的web站点根目录
			String ctx = "";
			if("true".equals(ConfigUtils.getPro("debug"))){
				ctx = request.getScheme() + "://" + domainname + ":" + request.getServerPort()+request.getContextPath();
			}else{
				ctx = request.getScheme() + "://" + domainname +request.getContextPath();
			}
			session.setAttribute("ctx", ctx);
		}
		return (String)session.getAttribute("ctx");
	}
}
